﻿using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace LaserAutoTest.UI.Adapters
{
    public class StackPanelRegionAdapter : RegionAdapterBase<StackPanel>
    {
        public StackPanelRegionAdapter(IRegionBehaviorFactory regionBehaviorFactory) : base(regionBehaviorFactory)
        {
        }


        protected override void Adapt(IRegion region, StackPanel regionTarget)
        {
            // 代码意思
            //动态设置他内容的一个代码区域
            //1、检测这个区域的视图动态变化
            region.Views.CollectionChanged += (s, e) =>
            {
                //判断他现在的行为是增加还是删除
                //如果是增加行为
                if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
                {
                    //循环他的数组，获取所有控件
                    foreach (FrameworkElement item in e.NewItems)
                    {
                        regionTarget.Children.Add(item);
                    }

                }

            };
        }

        protected override IRegion CreateRegion() => new Region();
    }
}
